package com.visutech.common.utils;

import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by 程振奇 on 2017/8/18.
 */
public class DateUtil extends DateUtils {
    /**
     * 获得两个日期之间的所有月份
     * @param minDate
     * @param maxDate
     * @return
     * @throws ParseException
     */
    private static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }

    /**
     * 获取日期的某个类型值
     * type 与Calendar.type一致
     * @param date
     * @param type
     * @return
     */
    public static Integer getInteger(Date date, int type){
        Integer result = null;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        result = calendar.get(type);
        if(type == Calendar.MONTH){
            result++;
        }
        return result;
    }


    /**
     *获取一个月的第一天
     * @param date
     * @return
     */
    public static Date getMonthFirst(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }


    /**
     * 获取一个月的最后一天
     * @param date
     * @return
     */
    public static Date getMonthLast(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        calendar.set(Calendar.MILLISECOND,999);
        return calendar.getTime();
    }

    /**
     * 获取一天中的开始时间
     * @param date
     * @return
     */
    public static Date getDayFirst(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }

    /**
     * 获取一天中的最后一天
     * @param date
     * @return
     */
    public static Date getDayLast(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        calendar.set(Calendar.MILLISECOND,999);
        return calendar.getTime();
    }

    /**
     * 获取两日期最大的日期
     * @param beginDate
     * @param endDate
     * @return
     */
    public static Date maxDate(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        }
        if (endDate == null) {
            return beginDate;
        }
        if (beginDate.after(endDate)) {
            return beginDate;
        }
        return endDate;
    }

    /**
     * 获取两日期最小日期
     * @param beginDate
     * @param endDate
     * @return
     */
    public static Date minDate(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        }
        if (endDate == null) {
            return beginDate;
        }
        if (beginDate.after(endDate)) {
            return endDate;
        }
        return beginDate;
    }


    /**
     * 计算两个时间的差
     * @param time1
     * @param time2
     * @param type
     * @return
     */
    public static Integer getDifferInteger(Date time1, Date time2, int type){
        Calendar c1 = Calendar.getInstance();
        c1.setTime(time1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(time2);
        long substract = Math.abs(time1.getTime() - time2.getTime());
        Integer result = null;
        switch (type){
            case Calendar.YEAR :
                result = Math.abs((c1.get(type) - c2.get(type)));
                break;
            case Calendar.MONTH:
                int years = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
                int month = c1.get(type) - c2.get(type);
                result = Math.abs(12*years+month);
                break;
            case Calendar.DATE:
                int ys = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
                int days = c1.get(Calendar.DAY_OF_YEAR) - c2.get(Calendar.DAY_OF_YEAR);
                result = Math.abs(ys*365+days);
                break;
            case Calendar.HOUR:
                result = Long.valueOf((substract/(60*60*1000))).intValue();
                break;
            case Calendar.MINUTE:
                result = Long.valueOf((substract/(60*1000))).intValue();
                break;
            case Calendar.SECOND:
                result = Long.valueOf(substract/1000).intValue();
                break;
            case Calendar.MILLISECOND:
                result = Long.valueOf(substract).intValue();
                break;
        }
        return  result;
    }


}


